﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agresso.Interface.Layer.Gateway;
using Agresso.Interface.Layer.Domain;
using System.Data.SqlClient;
using Agresso.Interface.Layer.SQL;
using System.Data;

namespace Agresso.Interface.Layer.Service
{
    public class apodettextService
    {
        DataBaseConnectivity m_dataBaseConectivity;
        public apodettextService()
        {
            m_dataBaseConectivity = new DataBaseConnectivity();
        }
        public bool IsExistOrderID(long orderID)
        {
            try
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Open();
                SqlCommand cmd = new SqlCommand(apodettextSQL.IsExistOrderID(), m_dataBaseConectivity.AgrTempConnectionSQLConnection);
                cmd.Parameters.Add("@order_id", SqlDbType.BigInt).Value = orderID;
                int result = Convert.ToInt32(cmd.ExecuteScalar());
                return result > 0;
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Close();
            }
        }
        public bool IsExistOrderID(long orderID, string client, int line_no, int sequence_no)
        {
            try
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Open();
                SqlCommand cmd = new SqlCommand(apodettextSQL.IsExistOrderIDClientLineSequenceNo(), m_dataBaseConectivity.AgrTempConnectionSQLConnection);
                cmd.Parameters.Add("@order_id", SqlDbType.BigInt).Value = orderID;
                cmd.Parameters.Add("@client", SqlDbType.NVarChar).Value = client;
                cmd.Parameters.Add("@line_no", SqlDbType.Int).Value = line_no;
                cmd.Parameters.Add("@sequence_no", SqlDbType.Int).Value = sequence_no;
                int result = Convert.ToInt32(cmd.ExecuteScalar());
                return result > 0;
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Close();
            }
        }
        public void Insert(string client, long order_id, int sequence_no, string short_info, string user_id, string voucher_type)
        {
            try
            {
                apodettext apd = new apodettext();
                apd.break_flag = "0";
                apd.client = client;
                apd.cur_amount = 0;
                apd.last_update = DateTime.Now;
                apd.line_no = 1;
                apd.order_id = order_id;
                apd.sequence_no = sequence_no;
                apd.short_info = short_info.Length > 60 ? short_info.Substring(1, 60) : short_info;
                apd.user_id = user_id;
                apd.voucher_type = voucher_type;

                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Open();
                SqlCommand cmd = new SqlCommand(apodettextSQL.Insert(), m_dataBaseConectivity.AgrTempConnectionSQLConnection);
                cmd.Parameters.Add("@break_flag", SqlDbType.NVarChar).Value = apd.break_flag;
                cmd.Parameters.Add("@client", SqlDbType.NVarChar).Value = apd.client;
                cmd.Parameters.Add("@cur_amount", SqlDbType.Decimal).Value = apd.cur_amount;
                cmd.Parameters.Add("@last_update", SqlDbType.DateTime).Value = apd.last_update;
                cmd.Parameters.Add("@line_no", SqlDbType.Int).Value = apd.line_no;
                cmd.Parameters.Add("@order_id", SqlDbType.BigInt).Value = apd.order_id;
                cmd.Parameters.Add("@sequence_no", SqlDbType.Int).Value = apd.sequence_no;
                cmd.Parameters.Add("@short_info", SqlDbType.NVarChar).Value = apd.short_info;
                cmd.Parameters.Add("@user_id", SqlDbType.NVarChar).Value = apd.user_id;
                cmd.Parameters.Add("@voucher_type", SqlDbType.NVarChar).Value = apd.voucher_type;
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Close();
            }
        }

        internal void UpdateOrderIDandStatus(long agrtid, long order_id, string break_flag)
        {
            try
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Open();
                SqlCommand cmd = new SqlCommand(apodettextSQL.UpdateOrderIDandStatus(), m_dataBaseConectivity.AgrTempConnectionSQLConnection);
                cmd.Parameters.Add("@agrtid", SqlDbType.BigInt).Value = agrtid;
                cmd.Parameters.Add("@order_id", SqlDbType.BigInt).Value = order_id;
                cmd.Parameters.Add("@break_flag", SqlDbType.NVarChar).Value = break_flag;
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Close();
            }
        }
    }
}
